package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpSellerCollectProductExpandBo;
import com.erp.gather.domain.vo.ErpSellerCollectProductExpandVo;
import com.erp.gather.mapper.ErpSellerCollectProductExpandMapper;
import com.erp.gather.service.IErpSellerCollectProductExpandService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpSellerCollectProductExpand;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 个人采集商品拓展Service业务层处理
 *
 * @author erp
 * @date 2023-06-20
 */
@RequiredArgsConstructor
@Service
public class ErpSellerCollectProductExpandServiceImpl implements IErpSellerCollectProductExpandService {

    private final ErpSellerCollectProductExpandMapper baseMapper;

    /**
     * 查询个人采集商品拓展
     */
    @Override
    public ErpSellerCollectProductExpandVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询个人采集商品拓展列表
     */
    @Override
    public TableDataInfo<ErpSellerCollectProductExpandVo> queryPageList(ErpSellerCollectProductExpandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerCollectProductExpand> lqw = buildQueryWrapper(bo);
        Page<ErpSellerCollectProductExpandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询个人采集商品拓展列表
     */
    @Override
    public List<ErpSellerCollectProductExpandVo> queryList(ErpSellerCollectProductExpandBo bo) {
        LambdaQueryWrapper<ErpSellerCollectProductExpand> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerCollectProductExpand> buildQueryWrapper(ErpSellerCollectProductExpandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerCollectProductExpand> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSkuProperty()), ErpSellerCollectProductExpand::getSkuProperty, bo.getSkuProperty());
        lqw.eq(bo.getAbsoluteQuoteType() != null, ErpSellerCollectProductExpand::getAbsoluteQuoteType, bo.getAbsoluteQuoteType());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsoluteQuoteMap()), ErpSellerCollectProductExpand::getAbsoluteQuoteMap, bo.getAbsoluteQuoteMap());
        lqw.eq(StringUtils.isNotBlank(bo.getFreightMap()), ErpSellerCollectProductExpand::getFreightMap, bo.getFreightMap());
        lqw.eq(bo.getIsFreight() != null, ErpSellerCollectProductExpand::getIsFreight, bo.getIsFreight());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPackSell()), ErpSellerCollectProductExpand::getIsPackSell, bo.getIsPackSell());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseUnit()), ErpSellerCollectProductExpand::getBaseUnit, bo.getBaseUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getAddUnit()), ErpSellerCollectProductExpand::getAddUnit, bo.getAddUnit());
        lqw.eq(bo.getAddWeight() != null, ErpSellerCollectProductExpand::getAddWeight, bo.getAddWeight());
        lqw.eq(bo.getBulkDiscount() != null, ErpSellerCollectProductExpand::getBulkDiscount, bo.getBulkDiscount());
        lqw.eq(bo.getBulkOrder() != null, ErpSellerCollectProductExpand::getBulkOrder, bo.getBulkOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrencyCode()), ErpSellerCollectProductExpand::getCurrencyCode, bo.getCurrencyCode());
        lqw.eq(bo.getDeliveryTime() != null, ErpSellerCollectProductExpand::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsImageDynamic()), ErpSellerCollectProductExpand::getIsImageDynamic, bo.getIsImageDynamic());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), ErpSellerCollectProductExpand::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords1()), ErpSellerCollectProductExpand::getProductMoreKeywords1, bo.getProductMoreKeywords1());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords2()), ErpSellerCollectProductExpand::getProductMoreKeywords2, bo.getProductMoreKeywords2());
        lqw.eq(bo.getLotNum() != null, ErpSellerCollectProductExpand::getLotNum, bo.getLotNum());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerMemberId()), ErpSellerCollectProductExpand::getOwnerMemberId, bo.getOwnerMemberId());
        lqw.eq(bo.getOwnerMemberSeq() != null, ErpSellerCollectProductExpand::getOwnerMemberSeq, bo.getOwnerMemberSeq());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageType()), ErpSellerCollectProductExpand::getPackageType, bo.getPackageType());
        lqw.eq(bo.getStock() != null, ErpSellerCollectProductExpand::getStock, bo.getStock());
        lqw.eq(StringUtils.isNotBlank(bo.getClassifyLabel()), ErpSellerCollectProductExpand::getClassifyLabel, bo.getClassifyLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), ErpSellerCollectProductExpand::getSummary, bo.getSummary());
        lqw.eq(bo.getProductStatus() != null, ErpSellerCollectProductExpand::getProductStatus, bo.getProductStatus());
        lqw.eq(bo.getEditStatus() != null, ErpSellerCollectProductExpand::getEditStatus, bo.getEditStatus());
        lqw.eq(bo.getIsOptimize() != null, ErpSellerCollectProductExpand::getIsOptimize, bo.getIsOptimize());
        lqw.eq(StringUtils.isNotBlank(bo.getAttributeId()), ErpSellerCollectProductExpand::getAttributeId, bo.getAttributeId());
        lqw.eq(bo.getBrandId() != null, ErpSellerCollectProductExpand::getBrandId, bo.getBrandId());
        lqw.eq(StringUtils.isNotBlank(bo.getProductAttribute()), ErpSellerCollectProductExpand::getProductAttribute, bo.getProductAttribute());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailKeyword()), ErpSellerCollectProductExpand::getDetailKeyword, bo.getDetailKeyword());
        lqw.eq(bo.getIsAneroidMarkup() != null, ErpSellerCollectProductExpand::getIsAneroidMarkup, bo.getIsAneroidMarkup());
        lqw.eq(bo.getIsOnlyBattery() != null, ErpSellerCollectProductExpand::getIsOnlyBattery, bo.getIsOnlyBattery());
        lqw.eq(bo.getIsContainsBattery() != null, ErpSellerCollectProductExpand::getIsContainsBattery, bo.getIsContainsBattery());
        lqw.eq(bo.getIsTranslate() != null, ErpSellerCollectProductExpand::getIsTranslate, bo.getIsTranslate());
        lqw.eq(bo.getIsUploadTest() != null, ErpSellerCollectProductExpand::getIsUploadTest, bo.getIsUploadTest());
        lqw.eq(StringUtils.isNotBlank(bo.getHsCode()), ErpSellerCollectProductExpand::getHsCode, bo.getHsCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerCollectProductExpand::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增个人采集商品拓展
     */
    @Override
    public Boolean insertByBo(ErpSellerCollectProductExpandBo bo) {
        ErpSellerCollectProductExpand add = BeanUtil.toBean(bo, ErpSellerCollectProductExpand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改个人采集商品拓展
     */
    @Override
    public Boolean updateByBo(ErpSellerCollectProductExpandBo bo) {
        ErpSellerCollectProductExpand update = BeanUtil.toBean(bo, ErpSellerCollectProductExpand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerCollectProductExpand entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除个人采集商品拓展
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
